import config from "./config.js"

let request = function(path,params,callback,server){
	$.ajax({
        url: (server?server:config.api_server)+path,
        contentType: "application/json;charset=utf-8",
        "X-Requested-With": null,
        data: JSON.stringify(params),
        type: 'POST',
        async: true,
        dataType: "json",
        success: function (res) {
            if(res.status!=0){
                if(res.status==403){
                      //URL中的页面ID
                    let pid=query("pid");
                    window.location="login.html"+(pid?("?pid="+pid):"");
                }
                else{
                    alert([res.status,res.msg]);
                    throw new Error(res.status+" "+res.msg);
                }
            }
            else{
                callback(res.data);
            }
        },
        error: function(req,msg,obj){
            throw new Error(msg);
        }
    })
};

function query(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

export default request;